package com.siteseat.crudeq.kernel;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class MessageId {

  private transient Queue queue;

  private Integer         queueId;

  private int             offset;

  private long            chunkId;

  /**
   * 
   *
   * @param mids
   * @return
   */
  public static Set<Integer>  extraceQueueIds( Collection<MessageId> mids ) {
    Set<Integer> r = new HashSet<Integer>( mids.size() );
    for( MessageId mid: mids ) {
      Integer qid = mid.getQueueId();
      r.add( qid );
    }
    return r;
  }
  
  /**
   * @param size
   */
  public MessageId( long chunkId, int offset, Integer queueId ) {
    this.chunkId = chunkId;
    this.offset = offset;
    this.queueId = queueId;
  }

  /**
   * Gets the queue
   * 
   * @return the queue
   */
  public Queue getQueue() {
    return queue;
  }

  /**
   * Sets the queue
   * 
   * @param queue
   *          the queue to set
   */
  public void setQueue( Queue queue ) {
    this.queue = queue;
  }

  /**
   * @param chunkId
   */
  public void reset( long cid ) {
    this.chunkId = cid;
    this.offset = 0;
  }

  /**
   * Gets the offset
   * 
   * @return the offset
   */
  public int getOffset() {
    return offset;
  }

  /**
   * Gets the queueId
   * 
   * @return the queueId
   */
  public Integer getQueueId() {
    return queueId;
  }

  /**
   * Gets the chunkId
   * 
   * @return the chunkId
   */
  public long getChunkId() {
    return chunkId;
  }

  public boolean olderThan( MessageId that ) {
    return this.chunkId < that.chunkId || ( this.chunkId == that.chunkId && this.offset < that.offset );
  }

  /**
   * Sets the queueId
   * 
   * @param queueId
   *          the queueId to set
   */
  public void setQueueId( Integer queueId ) {
    this.queueId = queueId;
  }

  /**
   * Sets the offset
   * 
   * @param offset
   *          the offset to set
   */
  public void setOffset( int offset ) {
    this.offset = offset;
  }

  /**
   * Sets the chunkId
   * 
   * @param chunkId
   *          the chunkId to set
   */
  public void setChunkId( long chunkId ) {
    this.chunkId = chunkId;
  }

}
